package com.imooc.demo.API;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class FFMpegTest{
    private String ffmpegEXE;
    public FFMpegTest(String ffmpegEXE){
        super();
        this.ffmpegEXE=ffmpegEXE;
    }

    public void convertor(String videoInputPath,String mp3InputPath,double seconds,String videoOutputPath)throws Exception {
        //ffmpeg -i input.mp4 output.avi
        //ffmpeg.exe -i 6.mp4 -i 6.mp3 -c:v copy -map 0:v -map 1:a -t 10 -y 4.mp4
        List<String> command = new ArrayList<>();
        command.add(ffmpegEXE);
        command.add("-i");
        command.add(videoInputPath);
        command.add("-i");
        command.add(mp3InputPath);
        command.add("-c:v");
        command.add("copy");
        command.add("-map");
        command.add("0:v");
        command.add("-map");
        command.add("1:a");
        command.add("-t");
        command.add(String.valueOf(seconds));
        command.add("-y");
        command.add(videoOutputPath);

//        for(String c:command){
//            System.out.println(c+" ");
//        }
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = builder.start();

        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {

        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }
    }


    /**
     * 生成视频封面，截取视频第一秒
     * @param videoInputPath
     * @param coverOutputPath
     * @throws IOException
     */
    public void getcover(String videoInputPath, String coverOutputPath) throws IOException {
        List<String> command = new ArrayList();
        command.add(ffmpegEXE);
        command.add("-ss");
        command.add("00:00:01");
        command.add("-y");
        command.add("-i");
        command.add(videoInputPath);
        command.add("-vframes");
        command.add("1");
        command.add(coverOutputPath);

        ProcessBuilder processBuilder = new ProcessBuilder(command);
        Process process = processBuilder.start();
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

        while (bufferedReader.readLine() != null) { }
        if (bufferedReader != null){
            bufferedReader.close();
        }if (inputStreamReader != null){
            inputStreamReader.close();
        }if (errorStream != null){
            errorStream.close();
        }
    }

//    public static void main(String[] args){
//        FFMpegTest ffmpeg=new FFMpegTest("D:\\ffmpeg\\bin\\ffmpeg.exe");
//        try{
//            //ffmpeg.convertor("D:\\ffmpeg\\bin\\6.mp4","D:\\ffmpeg\\bin\\6.mp3",10,"D:\\ffmpeg\\bin\\7.mp4");
//            ffmpeg.getcover("D:\\ffmpeg\\bin\\6.mp4","D:\\ffmpeg\\bin\\6.jpg");
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//    }
}
